package com.fpd.third;

import com.alibaba.fastjson.JSON;
import com.fpd.model.entity.TheRecordingParameters;

import com.fpd.tools.ToInterface;
import com.fpd.tools.UUIDUtil;
import org.junit.jupiter.api.Test;

import java.security.MessageDigest;
import java.time.Instant;
import java.util.Arrays;

/**
 * TODO
 *
 * @author jiangshuai
 * @version 1.0
 * @date 2020/11/17 18:53
 */
public class TheRecordingInterface {
    //传工号与被叫为参数
    /*
     *startTime  datetime (YYYYY-MM-DD HH:II:SS)  呼叫时间（开始时间）大于该时间
     *endTime   datetime (YYYYY-MM-DD HH:II:SS) 呼叫时间（开始时间）小于该时间
     *jobNumber string 工号
     * caller string 主叫
     * callee string 被叫
     */

    //开始时间+结束时间+工号+主叫+被叫
    public static  String jobNumberCallerCalle(String startTime,String endTime,String jobNumber, String caller, String callee){
        return TheRecordingInterface.recordingParameters(startTime, endTime, jobNumber, caller, callee);
    }




    //工号被叫为参数
    public static String recordingParametersCalle(String jobNumber,String callee){
      return TheRecordingInterface.recordingParameters("", "", jobNumber, "", callee);
    }
    //工号被叫为参数
    public static String jobNumber(String jobNumber){
        return TheRecordingInterface.recordingParameters("", "", jobNumber, "", "");
    }
    //只传开始时间与结束时间为参数
    public static String startTimeEndTime(String startTime, String endTime){
        return TheRecordingInterface.recordingParameters(startTime, endTime, "", "", "");
    }



    //返回录音接口参数
    public static String recordingParameters(String startTime, String endTime, String jobNumber, String caller, String callee){
        TheRecordingParameters theRecordingParameters = new TheRecordingParameters(startTime, endTime, jobNumber, caller, callee);
        String timeBO2 = JSON.toJSONString(theRecordingParameters);//转换成json
        String Secret="8b1doEIchXbwJTqlqIpL330bgbKDBtH9";//密钥
        String time= Instant.now().getEpochSecond()+"";//秒时间戳
        String uuid32 = UUIDUtil.createUUID32();//32位随机数
        String [] strs={Secret,timeBO2,time,uuid32};//所有参数放入string数组内
        Arrays.sort(strs);//对数组排序
        String str1="";//排序后连接成字符串
        for (String str : strs) {
            str1+=str;
        }
        String sha1 = HashKit1.sha1(str1);
        String sign = ToInterface.interfaceUtil("http://robot.rmlx.cn:8080/api/cdr/get_cdr_list?app_id=rm5fa10d86415a2u&timestamp=" + time + "&sign=" + sha1 + "&nonce=" + uuid32 + "&page=1", timeBO2);
        return sign;


    }
}


//HSA1算法
class HashKit1 {
    private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();

    public static String sha1(String srcStr){
        return hash("SHA-1", srcStr);
    }

    public static String hash(String algorithm, String srcStr) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
            return toHex(bytes);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toHex(byte[] bytes) {
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i=0; i<bytes.length; i++) {
            ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }

}